/**
 * 弹窗状态管理工具
 * 用于确保用户每次打开小程序只看到一次相关弹窗
 */

class ModalManager {
	constructor() {
		this.storagePrefix = 'modal_';
	}

	/**
	 * 检查并设置弹窗状态
	 * @param {string} key 弹窗标识
	 * @param {string} type 弹窗类型：'session'(会话期间), 'daily'(每日), 'permanent'(永久)
	 * @returns {boolean} 是否可以显示弹窗
	 */
	checkAndSetModalState(key, type = 'session') {
		const storageKey = this.storagePrefix + key;
		
		switch (type) {
			case 'session':
				// 会话期间只显示一次
				const sessionState = uni.getStorageSync(storageKey);
				if (sessionState) {
					return false;
				}
				uni.setStorageSync(storageKey, true);
				return true;
				
			case 'daily':
				// 每日只显示一次
				const today = new Date().toDateString();
				const lastShownDate = uni.getStorageSync(storageKey);
				
				if (lastShownDate === today) {
					return false;
				}
				uni.setStorageSync(storageKey, today);
				return true;
				
			case 'permanent':
				// 永久只显示一次
				const permanentState = uni.getStorageSync(storageKey);
				if (permanentState) {
					return false;
				}
				uni.setStorageSync(storageKey, true);
				return true;
				
			default:
				return true;
		}
	}

	/**
	 * 重置会话期间的弹窗状态
	 * @param {string} key 弹窗标识（可选，不传则重置所有）
	 */
	resetSessionModals(key = null) {
		try {
			if (key) {
				const storageKey = this.storagePrefix + key;
				uni.removeStorageSync(storageKey);
			} else {
				// 重置所有会话期间的弹窗
				const storageInfo = uni.getStorageInfoSync();
				if (storageInfo && storageInfo.keys) {
					storageInfo.keys.forEach(storageKey => {
						if (storageKey.startsWith(this.storagePrefix)) {
							uni.removeStorageSync(storageKey);
						}
					});
				}
			}
		} catch (error) {
			console.error('重置弹窗状态失败:', error);
		}
	}

	/**
	 * 清除指定弹窗状态
	 * @param {string} key 弹窗标识
	 */
	clearModalState(key) {
		const storageKey = this.storagePrefix + key;
		uni.removeStorageSync(storageKey);
	}

	/**
	 * 检查弹窗状态
	 * @param {string} key 弹窗标识
	 * @param {string} type 弹窗类型
	 * @returns {boolean} 是否已显示过
	 */
	hasShownModal(key, type = 'session') {
		const storageKey = this.storagePrefix + key;
		
		switch (type) {
			case 'session':
			case 'permanent':
				return !!uni.getStorageSync(storageKey);
				
			case 'daily':
				const today = new Date().toDateString();
				const lastShownDate = uni.getStorageSync(storageKey);
				return lastShownDate === today;
				
			default:
				return false;
		}
	}

	/**
	 * 获取弹窗状态信息
	 * @returns {Object} 弹窗状态信息
	 */
	getModalStates() {
		const storageInfo = uni.getStorageInfoSync();
		const modalStates = {};
		
		storageInfo.keys.forEach(storageKey => {
			if (storageKey.startsWith(this.storagePrefix)) {
				const key = storageKey.replace(this.storagePrefix, '');
				modalStates[key] = uni.getStorageSync(storageKey);
			}
		});
		
		return modalStates;
	}
}

// 创建单例实例
const modalManager = new ModalManager();

export default modalManager;

// 预定义的弹窗键名
export const MODAL_KEYS = {
	// 提现相关
	WITHDRAW_APPROVED: 'withdraw_approved',
	WITHDRAW_REJECTED: 'withdraw_rejected',
	WITHDRAW_TRANSFER_INITIATED: 'withdraw_transfer_initiated',
	WITHDRAW_SUCCESS: 'withdraw_success',
	WITHDRAW_FAILED: 'withdraw_failed',
	WITHDRAW_UNCONFIRMED_REMINDER: 'withdraw_unconfirmed_reminder',
	WITHDRAW_RECORDS_REMINDER: 'withdraw_records_reminder',
	
	// 提现确认相关
	WITHDRAW_CONFIRM_PREFIX: 'withdraw_confirm_', // 需要加上记录ID
	
	// 其他业务相关
	NEW_ORDER: 'new_order',
	AUTH_RESULT: 'auth_result',
	NOTIFICATION: 'notification'
}; 